package com.jprocms.module.cms.controller.admin.sensitivetask;

import com.jpro.framework.common.pojo.CommonResult;
import com.jpro.framework.common.pojo.PageResult;
import com.jpro.framework.excel.core.util.ExcelUtils;
import com.jpro.framework.security.core.LoginUser;
import com.jpro.framework.security.core.util.SecurityFrameworkUtils;
import com.jprocms.module.cms.controller.admin.sensitivetask.vo.TaskSensitiveResultExcelVO;
import com.jprocms.module.cms.controller.admin.sensitivetask.vo.TaskSensitiveResultListReqVO;
import com.jprocms.module.cms.controller.admin.sensitivetask.vo.TaskSensitiveResultPageReqVO;
import com.jprocms.module.cms.controller.admin.sensitivetask.vo.TaskSensitiveResultRespVO;
import com.jprocms.module.cms.convert.tasksensitiveresult.TaskSensitiveResultConvert;
import com.jprocms.module.cms.dal.dataobject.site.SiteDO;
import com.jprocms.module.cms.dal.dataobject.tasksensitiveresult.TaskSensitiveResultDO;
import com.jprocms.module.cms.service.content.ContentService;
import com.jprocms.module.cms.service.content.SensitiveWordService;
import com.jprocms.module.cms.service.sensitivetask.SensitiveTaskService;
import com.jprocms.module.cms.util.CmsContextUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static com.jpro.framework.common.pojo.CommonResult.success;
import static com.jprocms.module.cms.dal.dataobject.task.TaskDO.TASK_NAME_SCAN_SENSITIVEWORD;

@Tag(name = "管理后台 - 敏感词扫描任务结果")
@RestController
@RequestMapping("/cms/sensitive-task")
@Validated
public class SensitiveTaskController {

    @Resource
    private SensitiveTaskService sensitiveTaskService;
    @Resource
    private SensitiveWordService sensitiveWordService;
    @Resource
    private ContentService contentService;

    @PostMapping("/create")
    @Operation(summary = "创建敏感词扫描任务")
    @PreAuthorize("@ss.hasPermission('cms:task-sensitive-result:create')")
    public CommonResult<Boolean> createSensitiveCheckTask() {
        SiteDO site = CmsContextUtil.getSite();
        LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
        sensitiveWordService.checkContentSensitiveWord(site.getId(), loginUser.getId(), TASK_NAME_SCAN_SENSITIVEWORD, site);
        return CommonResult.success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除敏感词扫描任务")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('cms:task-sensitive-result:delete')")
    public CommonResult<Boolean> deleteTaskSensitiveResult(@RequestParam("id") Long id) {
        sensitiveTaskService.deleteTaskSensitiveResult(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得敏感词扫描任务")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('cms:task-sensitive-result:query')")
    public CommonResult<TaskSensitiveResultRespVO> getTaskSensitiveResult(@RequestParam("id") Long id) {
        TaskSensitiveResultDO taskSensitiveResult = sensitiveTaskService.getTaskSensitiveResult(id);
        return success(TaskSensitiveResultConvert.INSTANCE.convert(taskSensitiveResult));
    }

    @GetMapping("/list")
    @Operation(summary = "获得敏感词扫描任务列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('cms:task-sensitive-result:query')")
    public CommonResult<List<TaskSensitiveResultRespVO>> getTaskSensitiveResultList(@RequestParam("ids") Collection<Long> ids) {
        List<TaskSensitiveResultDO> list = sensitiveTaskService.getTaskSensitiveResultList(ids);
        return success(TaskSensitiveResultConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得敏感词扫描任务分页")
    @PreAuthorize("@ss.hasPermission('cms:task-sensitive-result:query')")
    public CommonResult<PageResult<TaskSensitiveResultRespVO>> getTaskSensitiveResultPage(@Valid TaskSensitiveResultPageReqVO pageVO) {
        pageVO.setSiteId(CmsContextUtil.getCurrSiteId());
        PageResult<TaskSensitiveResultDO> pageResult = sensitiveTaskService.getTaskSensitiveResultPage(pageVO);
        PageResult<TaskSensitiveResultRespVO> taskSensitiveResultRespVOPageResult = TaskSensitiveResultConvert.INSTANCE.convertPage(pageResult);
        if (taskSensitiveResultRespVOPageResult.getTotal() > 0) {
            List<TaskSensitiveResultRespVO> list = taskSensitiveResultRespVOPageResult.getList();
            for (TaskSensitiveResultRespVO taskSensitiveResultDO : list) {
                taskSensitiveResultDO.setContentTitle(contentService.getById(taskSensitiveResultDO.getContentId()).getTitle());
            }
        }
        return success(taskSensitiveResultRespVOPageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出敏感词扫描任务")
    @PreAuthorize("@ss.hasPermission('cms:task-sensitive-result:export')")

    public void exportTaskSensitiveResultExcel(@Valid TaskSensitiveResultListReqVO exportReqVO,
                                               HttpServletResponse response) throws IOException {
        List<TaskSensitiveResultDO> list = sensitiveTaskService.getTaskSensitiveResultList(exportReqVO);
        // 导出 Excel
        List<TaskSensitiveResultExcelVO> datas = TaskSensitiveResultConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "敏感词扫描任务.xls", "数据", TaskSensitiveResultExcelVO.class, datas);
    }

}
